package day17;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

//本类测试List接口迭代器的常用方法
public class TestList2 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("喜羊羊");
        list.add("美羊羊");
        list.add("懒羊羊");
        list.add("沸羊羊");
        list.add("小肥羊");
        list.add("肥羊卷");
        System.out.println(list);
        //方式1用for循环来实现数据的遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("===================");
        //方式2增强for循环来实现数据的遍历
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("===================");
        //方式3用迭代器来实现数据的遍历
        //1.得到迭代器对象
        Iterator<String> it = list.iterator();
        //2.通过刚刚获得迭代器循环迭代集合中的所有元素
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("===================");
        //方式4 L
        //1.得到迭代器对象
        ListIterator<String> lit = list.listIterator();
        //2.通过刚刚获得迭代器循环迭代集合中的所有元素
        while (lit.hasNext()) {
            System.out.println(lit.next());
        }
        System.out.println(list);
        System.out.println("===================");
        ListIterator<String> lit2 = list.listIterator();
        while (lit2.hasNext()) {
            System.out.println(lit2.next());
            if (!lit2.hasNext()){
                System.out.println("开始逆序遍历");
                while (lit2.hasPrevious()){
                    System.out.println(lit2.previous());

                }
                break;
            }
        }
    }
}
